//
//  ComponentsUtils.m
//  ios-supplier
//
//  Created by feynmanyuan on 15/5/3.
//  Copyright (c) 2015年 feynmanyuan. All rights reserved.
//

#import "ComponentsUtils.h"

@implementation ComponentsUtils
// 获取当前处于activity状态的view controller
- (UIViewController *)activityViewController
{
    UIViewController* activityViewController = nil;
    
    UIWindow *window = [[UIApplication sharedApplication] keyWindow];
    if(window.windowLevel != UIWindowLevelNormal)
    {
        NSArray *windows = [[UIApplication sharedApplication] windows];
        for(UIWindow *tmpWin in windows)
        {
            if(tmpWin.windowLevel == UIWindowLevelNormal)
            {
                window = tmpWin;
                break;
            }
        }
    }
    
    NSArray *subViews = [window subviews];
    for (UIView *view in subViews) {
        id nextResponder = [view nextResponder];
        if ([nextResponder isKindOfClass:[UIViewController class]]) {
            activityViewController = nextResponder;
        }
    }
    if (activityViewController == nil) {
        activityViewController = window.rootViewController;
    }
    return activityViewController;
}

+(CGRect)CGRectIntegralCenteredInRect:(CGRect)innerRect withRect:(CGRect)outerRect
{
    CGFloat originX = floorf((outerRect.size.width - innerRect.size.width) * 0.5f);
    CGFloat originY = floorf((outerRect.size.height - innerRect.size.height) * 0.5f);
    CGRect bounds = CGRectMake(originX, originY, innerRect.size.width, innerRect.size.height);
    return bounds;
}

+(void)emptyStringTips:(UIView*)view tips:(NSString*)tips{
    UILabel * label = [[UILabel alloc] init];
    label.font = [UIFont systemFontOfSize:TextFontSizeH3];
    label.text = tips;
    label.textColor = TextColorLight;
    label.tag = 99;
    
    CGSize tipsSize = [tips sizeWithAttributes:@{NSFontAttributeName: [UIFont systemFontOfSize:TextFontSizeH3]}];
    
    CGRect rect = [self CGRectIntegralCenteredInRect:CGRectMake(0, 0, tipsSize.width, tipsSize.height) withRect:view.frame];
    
    label.frame = rect;
    label.bounds = rect;
    
    [view addSubview:label];
}

+(void)cleanEmptyTips:(UIView*)view{
    for( UIView * v in view.subviews ) {
        if( v.tag == 99 ) {
            [v removeFromSuperview];
        }
    }
}
@end
